package com.service;

import com.domain.*;
import com.domain.BlogComment;
import com.github.pagehelper.PageHelper;
import com.repository.mapper.BlogArticleMapper;
import com.repository.mapper.CommentMapper;
import com.repository.mapper.StatisticMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SiteService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private BlogArticleMapper articleMapper;
    @Autowired
    private StatisticMapper statisticMapper;
    public void updateStatistics(BlogArticle article) {
        Statistic statistic = statisticMapper.selectStatisticWithArticleId(article.getId());
        statistic.setHits(statistic.getHits()+1);
        statisticMapper.updateArticleHitsWithId(statistic);
    }
    public List<BlogComment> recentComments(int limit) {
        PageHelper.startPage(1, limit>10 || limit<1 ? 10:limit);
        List<BlogComment> byPage = commentMapper.selectNewComment();
        return byPage;
    }
    public List<BlogArticle> recentArticles(int limit) {
        PageHelper.startPage(1, limit>10 || limit<1 ? 10:limit);
        List<BlogArticle> list = articleMapper.selectArticleWithPage();
        for (int i = 0; i < list.size(); i++) {
            BlogArticle article = list.get(i);
            Statistic statistic = statisticMapper.selectStatisticWithArticleId(article.getId());
            article.setHits(statistic.getHits());
            article.setCommentsNum(statistic.getCommentsNum());
        }
        return list;
    }
    public StaticticsBo getStatistics() {
        StaticticsBo staticticsBo = new StaticticsBo();
        Integer articles = articleMapper.countArticle();
        Integer comments = commentMapper.countComment();
        staticticsBo.setArticles(articles);
        staticticsBo.setComments(comments);
        return staticticsBo;
    }


}
